//
//  CNOnlinePlayerController.m
//  chuiniu
//
//  Created by zheng ji on 14-9-30.
//  Copyright (c) 2014年 jxw. All rights reserved.
//

#import "CNOnlinePlayerController.h"
#import "CNPlayer.h"
#import "CommandDef.h"
#import "CNAppDelegate.h"
#import "CNGameUtil.h"
#import "CNViewController.h"
@interface CNOnlinePlayerController ()
{
    NSMutableArray* _arrayPlayers;
    int _startRecordNo;
    CNGameClient* _gameClient;
    CNAppDelegate* _appDelegate;
    iToast *toast;
    CNPlayer* _currentPlayer;
    NSString* _suInvitePlayerID;//当前邀请的对家ID
}
@end

@implementation CNOnlinePlayerController

- (void)viewDidLoad {
    [super viewDidLoad];
   // self.tableView.dataSource=self;
   // self.tableView.delegate=self;
    _startRecordNo=0;
    
    _appDelegate=(CNAppDelegate*)[UIApplication sharedApplication].delegate;
    _gameClient=_appDelegate.gameClient;
    _gameClient.gameMainViewController=self;
    
    _currentPlayer=[CNGameUtil getCurrentPlayerFromUserDefaults];
    
}
-(void)viewDidAppear:(BOOL)animated{
    [super viewDidAppear:animated];
   
    [self loadOnlinePlayerFromNet];
    
}

-(void)hiddenToastView{
    if(toast !=nil){
        [toast hideToast:nil];
        toast=nil;
    }
    
}
-(void)showToast:(int)tag title:(NSString*)title duration:(ToastDuration)duration position:(ToastPosition)position{
    [self showToast:tag title:title duration:duration position:position countDown:YES];
}
-(void)showToast:(int)tag title:(NSString*)title duration:(ToastDuration)duration position:(ToastPosition)position countDown:(BOOL)countDown{
    
    [self hiddenToastView];
    toast = [iToast makeToast:title];
    toast.tag=tag;
    toast.toastDelegate=self;
    toast.countDown=countDown;
    [toast setToastPosition:position];
    [toast setToastDuration:duration];
    [toast show];
}

//获取在线用户
-(void)loadOnlinePlayerFromNet{
    CNPlayer* p=[CNGameUtil getCurrentPlayerFromUserDefaults];
    NSString* playerId=p==nil?@"":p.playerID;
    
    [_gameClient sendOnlinePlayerCommand:playerId startRecordNo:_startRecordNo endRecordNo:_startRecordNo + 50];
    [self showToast:-1 title:@"正在获取在线玩家 " duration:kToastDuration15Second position:kToastPositionCenter ];
    
}
//增加在线玩家
-(void)addOnlinePlayerList:(NSMutableArray*)arrayPlayers{
    [self hiddenToastView];
    if(arrayPlayers==nil) return ;
    _arrayPlayers=arrayPlayers;
    [self.tableView reloadData];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {

    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {

    return _arrayPlayers==nil?0:_arrayPlayers.count;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"reuseIdentifier" forIndexPath:indexPath];
    
    
    CNPlayer* player=[_arrayPlayers objectAtIndex:indexPath.row];
    
    NSString* status=player.playing?@"对战中":@"空闲";
    
    cell.textLabel.text=[NSString stringWithFormat:@"%@               %@",player.playerName,status];
    cell.textLabel.textColor=[UIColor blueColor];
    cell.detailTextLabel.text=[NSString stringWithFormat:@"%@(%@金币)",player.levelName,player.coins];
    
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    CNPlayer* vsPlayer=[_arrayPlayers objectAtIndex:indexPath.row];
    _suInvitePlayerID=vsPlayer.playerID;
    
    [_gameClient sendSelectPlayerCommand:_currentPlayer.playerID vsPlayerID:vsPlayer.playerID];
    
    
    
    
   [self showToast:tagWaitSelectPlayer title:[NSString stringWithFormat:@"等待[%@]响应 ",vsPlayer.playerName] duration:kToastDuration15Second position:kToastPositionCenter];
}
/**
   提示信息倒计时结束
 */
-(void)toastTimeOver:(int)toastTag{
    if(toastTag==tagWaitSelectPlayer){
        
        _suInvitePlayerID=nil;
        
       [self showToast:-1 title:@"对家未响应,请选择其他玩家" duration:kToastDuration2Second position:kToastPositionCenter countDown:NO];
    }
}

//接受对战请求
-(void)setVSPlayer:(CNPlayer*)vsPlayer{
    [toast hideToast:nil];
    if(vsPlayer==nil) return;
    _appDelegate.vsPlayer=vsPlayer;
    
    CNViewController* viewCtrl=(CNViewController*)[ self.storyboard instantiateViewControllerWithIdentifier:@"CNViewController"];
    _appDelegate.gamePlayModel=GameModelMutilPlayer;
    viewCtrl.gameModel=GameModelMutilPlayer;
    viewCtrl.gameStatus=GameStatusNormal;

    _appDelegate.gameClient.gameMainViewController=viewCtrl;
    [self presentViewController:viewCtrl animated:YES completion:nil];
    
}

@end
